﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class EditPStock : BasePage
    {
        private IStockBLL stockBLL = IocContainer.Instance.Resolve<IStockBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int stockId = 0;
                    int.TryParse(Request.QueryString["StockId"], out stockId);
                    if (stockId > 0)
                    {
                        DataTable dt = stockBLL.Query(string.Format(" and FitemId={0}", stockId));
                        if (dt.Rows.Count > 0)
                        {
                            DataRow dr = dt.Rows[0];
                            this.txtStockCode.Text = dr["FNumber"].ToString();
                            this.txtStockName.Text = dr["FName"].ToString();
                            base.BinDropDownList<DropDownList>(this.ddlstStockType, dr["FType"].ToString(), true);
                            this.hidDepartId.Value = dr["FDeptId"].ToString();
                            if (dr["FType"].ToString() == "1")//客户
                            {
                                this.txtCustName.Value = dr["DepartmentName"].ToString();
                            }
                            else
                            {
                                this.txtDepartId.Value = dr["DepartmentName"].ToString();
                            }
                            this.txtContact.Text = dr["FFrelation"].ToString(); //model.FFrelation;
                            this.txtStocker.Text = dr["FSrelation"].ToString(); //model.FSrelation;
                            this.txtTel.Text = dr["FTel"].ToString(); //model.FTel;
                            this.txtMobile.Text = dr["FMobil"].ToString();// model.FMobil;
                            this.txtAddress.Text = dr["FDree"].ToString(); //model.FDree;
                        }
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                Skyworth.CRM.Service.Entity.Stock model = new Skyworth.CRM.Service.Entity.Stock();
                model.FNumber = this.txtStockCode.Text.Trim();
                model.FName = this.txtStockName.Text.Trim();
                model.FType = Convert.ToInt32(ddlstStockType.SelectedValue);
                model.FDeptId = Convert.ToInt32(this.hidDepartId.Value.Trim());
                model.FFrelation = this.txtContact.Text.Trim();
                model.FSrelation = this.txtStocker.Text.Trim();
                model.FTel = this.txtTel.Text.Trim();
                model.FMobil = this.txtMobile.Text.Trim();
                model.FDree = this.txtAddress.Text.Trim();
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    stockBLL.Add(model);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int stockId = 0;
                    int.TryParse(Request.QueryString["StockId"], out stockId);
                    model.FitemId = stockId;
                    stockBLL.Update(model);
                    isOK = true;
                }
                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}